package lc;

import java.util.*;

import org.junit.Test;

public class Ex159 {
    //至多包含两个字符的最长子串
    public String longestSubstrContainsTwo(String s) {
        char[] cs = s.toCharArray();
        int lo = 0, hi = 0, len = cs.length, max = 0, cnt = 0;
        String res = "";
        int[] map = new int[26];
        while (hi < len) {
            if (map[cs[hi] - 'a'] == 0) {
                cnt++;
            }
            map[cs[hi] - 'a']++;
            while (cnt > 2) {
                map[cs[lo] - 'a']--;
                if (map[cs[lo] - 'a'] == 0) {
                    cnt--;
                }
                lo++;
            }
            if (hi - lo + 1 > max) {
                res = s.substring(lo, hi + 1);
                max = hi - lo + 1;
            }
            hi++;
        }
        return res;
    }

    @Test
    public void test() {
        System.out.println(longestSubstrContainsTwo("ccaabbb"));
    }
}
